const { loginConfig,authorizeUrl,DBKEY } = require("../config");
const { isLogin,db } = require("../utils")
const log = require("../utils/log")
const open = require('open');
const { getToken } = require("../service/login");
let interval = null;
module.exports = async function(args) {
  // 判断是否登录
  if ((await isLogin())) {
    log.success('已经登录成功!!!')
  }else{
    // 没登录
    await open(authorizeUrl);

    let searchYQ = async ()=>{
        try {
          const res = await getToken();
          const data = res && res.data;
          const access_token = data.access_token;
          // console.log(res)
          if (access_token) {
            clearInterval(interval);
            // 存储起来
            await db.setVal(DBKEY, res.data)
            log.success('授权成功')
            process.exit();

          }
        } catch (error) {
          // console.log(error)
        }
    }
    interval = setInterval(searchYQ, 1500);

    setTimeout(() => {
      clearInterval(interval);
      log.danger('登录超时～')
    },60000);
  }
}